package org.ssc.cinema.controller;

import org.ssc.cinema.entity.Cinema;
import org.ssc.cinema.entity.CinemaTicket;
import org.ssc.cinema.entity.Tickethouse;
import org.ssc.cinema.feign.TickethouseFeign;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;

@RestController
@RequestMapping("/cinema")
public class CinemaHandler {

    @Resource
    TickethouseFeign tickethouseFeign;

    /**
     * 登录
     * @param cname 电影院名称
     * @param password 密码
     * @param session
     * @return
     */
    @PostMapping("/login")
    public String login(@RequestParam("cname") String cname,
                        @RequestParam("password") String password,HttpSession session){
        Object object = tickethouseFeign.cinemaLogin(cname,password);
        if(object != null){
            LinkedHashMap<String, Object> hashMap = (LinkedHashMap) object;
            Cinema cinema = new Cinema();
            cinema.setCid((int)hashMap.get("cid"));
            session.setAttribute("cinema",cinema);
            return "影院登录成功";
        }else{
            return "影院登录失败";
        }
    }

    /**
     * 登出
     * @param session 清理session
     * @return
     */
    @GetMapping("/logout")
    public String logout(HttpSession session){
        session.invalidate();
        return "影院登出";
    }

    /**
     * 排片（需要登录）
     * @param tickethouse  json对象见resources目录
     * @param session
     * @return
     */
    @PostMapping("/screen")
    public String screen(@RequestBody Tickethouse tickethouse, HttpSession session){
        Cinema c = (Cinema)session.getAttribute("cinema");
        if (c == null){
            return "请先登录";
        }else{
            tickethouse.setCid(c.getCid());
            tickethouse.setStandby(tickethouse.getTicketnum());
            int result = tickethouseFeign.screen(tickethouse);
            if(result == 1)return "排片成功";
            else return "排片失败";
        }
    }

    /**
     * 根据电影名称查看场次信息（返回字段与Audience不同）
     * @param moviename 电影名称
     * @return
     */
    @GetMapping("getTicketsByMoviename/{moviename}")
    public List<CinemaTicket> getTicketsByMoviename(@PathVariable("moviename") String moviename){
        List<Tickethouse> tickethouseList = new ArrayList<>();
        tickethouseList = tickethouseFeign.getTicketsByMoviename(moviename);
        List<CinemaTicket> cinemaTicketList = new ArrayList<>();
        if(tickethouseList.size() != 0) {
            for (Tickethouse t:tickethouseList) {
                CinemaTicket c = new CinemaTicket();
                c.setMoviename(t.getMoviename());
                c.setDate(t.getDate());
                c.setNumber(t.getNumber());
                c.setHall(t.getHall());
                c.setTicketnum(t.getTicketnum());
                c.setStandby(t.getStandby());
                c.setOccupancy(1-((double)t.getStandby()/(double)t.getTicketnum()));
                cinemaTicketList.add(c);
            }
        }
        return cinemaTicketList;
    }

}
